package cn.edu.ncu.housinghub.controller.landlord;

import cn.edu.ncu.housinghub.dto.request.house.HouseInfoUpdateRequest;
import cn.edu.ncu.housinghub.dto.request.house.HouseRequest;
import cn.edu.ncu.housinghub.dto.request.house.PropertyVerificationRequest;
import cn.edu.ncu.housinghub.entity.house.ComplementaryFacilities;
import cn.edu.ncu.housinghub.service.HouseService;
import cn.edu.ncu.housinghub.vo.Result;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/landlord")
public class LandlordController {

    @Autowired
    private HouseService houseService;


    /**
     * 提交房源认证
     */
    @PostMapping("/submitVerification")
    public Result submitVerification(@Valid @RequestBody PropertyVerificationRequest propertyVerificationRequest) {
        houseService.submitPropertyVerification(propertyVerificationRequest);
        return Result.success();
    }

    /**
     * 发布房源
     */
    @PostMapping("/publish")
    public Result publish(@Valid @RequestBody HouseRequest houseRequest) {
        houseService.publishHouseAfterVerification(houseRequest);
        return Result.success();
    }

    /**
     * 房东查询自己房源的认证信息
     */
    @GetMapping("/getPropertyVerificationByUserId/{userId}")
    public Result getPropertyVerificationByUserId(@PathVariable("userId") Long userId) {
        return Result.success(houseService.findPropertyVerificationByUserId(userId));
    }

    /**
     * 房东查询自己发布的房源
     */
    @GetMapping("/getHouseByUserId/{userId}")
    public Result getHouseByUserId(@PathVariable("userId") Long userId) {
        return Result.success(houseService.findHouseByLandLordId(userId));
    }

    /*
     * 房东删除已被驳回的房源验证
     */
    @PostMapping("/cancelVerification/{userId}/{propertyVerifyId}")
    public Result cancelVerification(@PathVariable("propertyVerifyId") Long propertyVerifyId,@PathVariable("userId")Long userId) {
        houseService.modifyPropertyVerificationDeleted(propertyVerifyId, userId);
        return Result.success();
    }

    /*
     * 房东批量删除已被驳回的房源验证
     */
    @PostMapping("/cancelBatchVerification/{userId}")
    public Result cancelBatchVerification(@RequestBody List<Long> propertyVerifyIds, @PathVariable Long userId) {
        houseService.modifyBatchPropertyVerificationDeleted(propertyVerifyIds, userId);
        return Result.success();
    }

    /**
     * 房东查询房产认证状态
     */
    @GetMapping("/getPropertyVerifyStatusByPropertyNo/{propertyNo}")
    public Result getPropertyVerifyStatusByPropertyNo(@PathVariable("propertyNo") String propertyNo) {
        return Result.success(houseService.findPropertyVerifyStatusByPropertyNo(propertyNo));
    }

    /**
     * 房东查询房产认证驳回原因
     */
    @GetMapping("/getPropertyVerifyRefuseReasonByPropertyNo/{propertyNo}")
    public Result getPropertyVerifyRefuseReasonByPropertyNo(@PathVariable("propertyNo") String propertyNo) {
        return Result.success(houseService.findPropertyVerifyRefuseReasonByPropertyNo(propertyNo));
    }

    /**
     * 房东取消房产认证
     */
    @PostMapping("/cancelPropertyVerify/{propertyNo}")
    public Result cancelPropertyVerify(@PathVariable("propertyNo") String propertyNo) {
        houseService.modifyCancelPropertyVerifyByPropertyNo(propertyNo);
        return Result.success();
    }

    /**
     * 房东更新房产信息
     * @param houseInfoUpdateRequest
     * @return
     */
    @PostMapping("/updateHouseInfo")
    public Result updateHouseInfo(@RequestBody@Valid HouseInfoUpdateRequest houseInfoUpdateRequest) {
        houseService.modifyHouseInfo(houseInfoUpdateRequest);
        return Result.success();
    }

    /**
     *房东根据房产ID更新配套设施信息
     * @param houseId
     * @param complementaryFacilities
     * @return
     */
    @PostMapping("/updateComplementaryFacilities/{houseId}")
    public Result updateComplementaryFacilities(@PathVariable("houseId") Long houseId,@RequestBody @Valid ComplementaryFacilities complementaryFacilities){
        houseService.modifyComplementaryFacilities(houseId,complementaryFacilities);
        return Result.success();
    }

    /**
     * 房东根据房产ID下架或上架房源
     * @param houseId 房产ID
     * @param houseStatus 0=上架重新为待出租状态，2=下架
     * @return
     */
    @PostMapping("/updateHouseStatus/{houseId}")
    public Result updateHouseStatus(@PathVariable("houseId") Long houseId,@RequestParam("houseStatus") Integer houseStatus){
        houseService.modifyHouseStatus(houseId,houseStatus);
        return Result.success();
    }

    /**
     * 房东根据房产ID查询房产信息
     * @param houseId 房产ID
     * @return 房产信息
     */
    @GetMapping("/getHouseByHouseId/{houseId}")
    public Result getHouseByHouseId(@PathVariable("houseId") Long houseId){
        return Result.success(houseService.findHouseByHouseId(houseId));
    }


}